package com.controller;

import com.domain.PayEntity;
import com.dto.AlipayBean;
import com.dto.JsonResponse;
import com.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付表 前端控制器
 * </p>
 *
 * @author sly
 * @since 2024-01-07
 */
@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private PayService payService;

    // 生成支付宝支付界面
    @PostMapping(value = "/alipay")
    public String alipay(@RequestBody AlipayBean alipayBean) {
        return payService.aliPay(alipayBean);
    }

    // 生成微信支付二维码
    @PostMapping(value = "/createWeixinQr")
    public Map<String, String> createWeixinQr(@RequestBody PayEntity payEntity) {
        return payService.createWeixinQr(payEntity);
    }

    @PostMapping(value = "/alipaySuccess")
    public void alipaySuccess(String payId) {
        System.out.println("回调来了");
        payService.alipaySuccess(payId);
    }

    // 只有在公网上才能回调
    @PostMapping(value = "/weixinSuccess")
    public void weixinSuccess(String payResult) {
        payService.weixinSuccess(payResult);
    }

    /**
     * 查询支付结果
     */
    @GetMapping("/getPayResult")
    public JsonResponse<String> getPayResult(@RequestParam String payId) {
        String result = payService.getPayResult(payId);
        return new JsonResponse<>(result);
    }

}
